//
//  FLLMaterialTableViewCell.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/8.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLMaterialTableViewCell.h"
#import "FLLMaterialCollectionViewCell.h"
#import "FLLMacro.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"


@interface FLLMaterialTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, waterCollectionFlowLayout>

@end

@implementation FLLMaterialTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUp];
    }
    return self;
}
- (void)awakeFromNib {
    
    
}
- (void)setUp
{

    self.grayLabel = [[UILabel alloc] init];
    self.grayLabel.backgroundColor = FLLLightGrayColor;
    [self.contentView addSubview:_grayLabel];

    [self createMaterialCollectionView];
    
}
- (void)createMaterialCollectionView
{

    self.materialLayout = [[FLLMaterialViewFlowLayout alloc] init];
    _materialLayout.delegate = self;
    
    
    
    self.materialCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_materialLayout];
    [self.contentView addSubview:_materialCollectionView];
    self.materialCollectionView.delegate = self;
    self.materialCollectionView.dataSource = self;
    _materialCollectionView.backgroundColor = [UIColor whiteColor];
    [_materialCollectionView registerNib:[UINib nibWithNibName:@"FLLMaterialCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"materialCollectionCell"];
    
}
- (void)layoutSubviews
{
    
    [super layoutSubviews];
    
    [_grayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(10);
    }];
    
    [_materialCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(_grayLabel.mas_top);
    }];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return _itemArray.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    FLLMaterialCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"materialCollectionCell" forIndexPath:indexPath];
    cell.dishNameLabel.textColor = [UIColor grayColor];
    cell.dishNameLabel.font = [UIFont systemFontOfSize:15 * kWidthScale];
    if (indexPath.row == 0) {
        cell.dishNameLabel.text = nil;
        [cell.dishImageView sd_setImageWithURL:[NSURL URLWithString:[_itemArray[indexPath.row] image]] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        }];
        
    } else {
 
       cell.dishNameLabel.text = _itemArray[indexPath.row].text;
        cell.dishImageView.image = [UIImage new];
    }
    cell.contentView.backgroundColor = FLLLightGrayColor;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CGFloat itemWidth = (_materialCollectionView.bounds.size.width - 6 * CellMargin_10) / 5.0;
    CGFloat itemHeight = itemWidth / 1.5;
    if (indexPath.row == 0) {
        return CGSizeMake(2 * itemWidth + CellMargin_10, 2 * itemHeight + CellMargin_10);
    }
    
    return CGSizeMake(itemWidth, itemHeight);
}
// 改变行 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return CellMargin_10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
   return CellMargin_10;
}
- (void)setItemArray:(NSArray<MaterialThirdData *> *)itemArray
{

    _itemArray = itemArray;
    [self.materialCollectionView reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row != 0) {
        MaterialThirdData *pushDic = (MaterialThirdData *)_itemArray[indexPath.row];
        self.pushBlock(pushDic);
    }
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
